package com.daoeping.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.daoeping.dao.UserDao;
import com.daoeping.domain.OtherData;
import com.daoeping.domain.Usr_user;
import com.daoeping.service.UserService;
import com.daoeping.util.ConverntJsonData;
import com.daoeping.util.Md5Password;
import com.daoeping.util.User;
import com.opensymphony.xwork2.ActionContext;

public class UserServiceImpl implements UserService {
	private UserDao userDao;
	
	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	@Override
	public boolean addUser(Usr_user user  ){
		user.setEmail(user.getUsername());
		user.setPassword(new Md5Password().setmd5Pass(user.getPassword()));
			if(userDao.insertData(user)){
				return true;
			}else{
				return false;
			}
	}

	@Override
	public boolean removeUser(Usr_user user) {
		if(userDao.deleteData(user)){
			return true;
		}else{
			return false;
		}
	}

	@Override
	public boolean modifyUser(Usr_user user){
		if(userDao.updateData(user)){
			return true;
		}else{
			return false;
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<User> findAllUser() {
		return (List<User>)userDao.selectDataByHQL("from Usr_user");
	}
	
	


	@SuppressWarnings("unchecked")
	@Override
	public String checkUserExist(Usr_user user)  {
		
		StringBuilder sb = new StringBuilder();
		sb.append("select user.id , user.username , user.password , user.email from Usr_user user");
		boolean flag = false;
		if(user!=null&&(!("").equals(user.getUsername()))&&user.getUsername()!=null){
			sb.append(" where username = '"+user.getUsername()+"'");
			flag = true;
		}
		if(user!=null&&(!("").equals(user.getPassword()))&&user.getPassword()!=null){
			if(flag){
				sb.append(" and ");
				sb.append(" password = '"+new Md5Password().setmd5Pass(user.getPassword())+"'");
			}
		}
		List<Object[]> userlist = (List<Object[]>) userDao.selectDataByHQL(sb.toString());
		List<String> list = new ArrayList<String>();
		if(userlist.size()!=0&&userlist.size()==1){
			list.add("exist");
			ActionContext.getContext().getSession().put("userid",userlist.get(0)[0] );
		}else {
			list.add("notexist");
		}
		ActionContext.getContext().getSession().put("companylevel", "No");
		return new ConverntJsonData().getJsonData(list).toString() ; 
	}

	@Override
	public String checkVerifyCodeExist(OtherData otherData) {
		String str = ActionContext.getContext().getSession().get("verifycode").toString();
		List<String> list = new ArrayList<String>();
		if(!"".equals(str)&&str.equals(otherData.getVerifycode())){
			list.add("exist");
		}else {
			list.add("notexist");
		}
		return new ConverntJsonData().getJsonData(list).toString() ; 
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public String checkUserCopExist(Usr_user user) {
		List<Object[]> userlist = (List<Object[]>) userDao.selectDataByHQL("select user.id , user.username , copinfo.name  from Usr_user user,Cop_companyuser cop , Cop_companyinfo copinfo where cop.cop_companyinfo.id = copinfo.id  and user.id = cop.usr_user.id and cop.usr_user.id = (select user.id from Usr_user user where user.username = '"+user.getUsername()+"' AND user.password = '"+new Md5Password().setmd5Pass(user.getPassword())+"')" );
		List<String> list = new ArrayList<String>();
		if(userlist.size()!=0&&userlist.size()==1){
			list.add("exist");
			ActionContext.getContext().getSession().put("companylevel", "Yes");
			ActionContext.getContext().getSession().put("userid",userlist.get(0)[0] );
			ActionContext.getContext().getSession().put("username",userlist.get(0)[1] );
		}else {
			list.add("notexist");
		}
		
		return new ConverntJsonData().getJsonData(list).toString() ; 
	}
}
